<template>
  <div class="textCon">
    <div class="center-box animate__animated animate__rotateIn">
      <div class="words">今天是认识仙女小姐姐的第</div>
      <div class="days-number">{{ day }}天</div>
      <div class="seconds">{{ hours }} 时 {{ minutes }} 分 {{ seconds }} 秒</div>
    </div>
    <audio src="@/assets/music/music.flac" class="audio" autoplay></audio>

    <div clas="button3">
      <span>立即下载</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      date: new Date('2022/01/19 16:05:00').getTime(),
      day: '',
      hours: '',
      minutes: '',
      seconds: '',
      timer: null,
      isPlay: false
    }
  },
  watch: {
    isPlay(val) {
      if (val) {
        this.playAudio()
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.init()

      setTimeout(() => {
        this.isPlay = true
      }, 1000)

      setInterval(() => {
        this.computedDate()
      }, 1000)
      
    })
  },
  methods: {
    playAudio() {
      const audio = document.querySelector('audio')
      setTimeout(() => {
        audio.play()
      }, 1000)
    },
    // playAudio () {
    //   const audio = document.querySelector('audio')
    //   setTimeout(() => {
    //     audio.addEventListener('canplaythrough', () => {
    //       audio.play()
    //     })
    //   }, 3000)
    // },
    init () {
      var box = document.getElementsByClassName('textCon')[0];
      var width = box.offsetWidth;
      var count = parseInt(width / 50 * 5);
      this.computedDate()
      for (var i = 0; i < count; i++) {
          var size = parseInt(this.ran(120, 180) / 10);
          var ele = document.createElement('div');
          ele.classList.add('item');
          ele.style.width = size + 'px';
          ele.style.height = size + 'px';
          ele.style.left = this.ran(0, 95) + '%';
          ele.style.top = this.ran(20, 80) + '%';
          ele.style.animationDelay = this.ran(0, 30) / 10 + 's';
          box.appendChild(ele);
      }
    },
    ran(min, max) {
      min = parseInt(min);
      max = parseInt(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    computedDate(){
      var distance = new Date().getTime() - this.date;//时间差的毫秒数
      var day = Math.floor(distance / (24 * 3600 * 1000));//计算出相差天数
      var leave1=distance%(24*3600*1000)    //计算天数后剩余的毫秒数
      var hours=Math.floor(leave1/(3600*1000))//计算出小时数
      //计算相差分钟数
      var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
      var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
      //计算相差秒数
      var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
      var seconds=Math.round(leave3/1000)
      console.log(day, hours, minutes, seconds, '===')
      this.day = day
      this.hours = hours
      this.minutes = minutes
      this.seconds = seconds
    }
  }
}
</script>

<style>
*{
    margin: 0;
    padding:0;
}
html,body{
    width:100%;
    height:100%;
    /*text-align: center;*/
    background-color: #ffc5b3;
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    -webkit-transform:translate(-50%,-50%);
    -ms-transform:translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    -o-transform:translate(-50%,-50%);
	background-image: radial-gradient(#ffeded, #ffc7c7);
}
.textCon{
  display: flex;
  flex-direction: column;
}
.text{
    font-size:48px;
}
.heart{
    width:80px;
    height:80px;
    background-color:#cc2a5d;
    margin:200px auto;
    position: relative;
    transform: rotate(45deg);
    animation: heart 1s ease-in infinite;
}
.heart::before,
.heart::after{
    content:'';
    width:100%;
    height:100%;
    background-color:#cc2a5d;
    border-radius: 50%;
}
.heart::before{
    position: absolute;
    top:0;
    left:0;
    transform: translateX(-50%);
}
.heart::after{
    position: absolute;
    top:0;
    left:0;
    transform: translateY(-50%);
}
@keyframes heart{
    0%{
        transform: scale(0.8) rotate(45deg);
        opacity: 0.8;
    }
    50%{
        transform: scale(1) rotate(45deg);
        opacity: 1;
    }
    100%{
        transform: scale(0.8) rotate(45deg);
        opacity: 0.8;
    }
}
.textCon .item{
    position: absolute;
    opacity: 0;
    background-color:#cc2a5d;
    transform: rotate(45deg);
    animation: hearts 3s ease-in infinite;
}
.textCon .item::before,
.textCon .item::after{
    content:'';
    width:100%;
    height:100%;
    background-color:#cc2a5d;
    position: absolute;
    top:0;
    left:0;
    border-radius: 50%;
}
.textCon .item::before{
    transform: translateX(-50%);
}
.textCon .item::after{
    transform: translateY(-50%);
}
@keyframes hearts{
    0%{
        opacity: 0;
        transform: translateY(0%) rotate(45deg);
    }
    20%{
        opacity: 0.8;
        transform: translateY(-20%) rotate(45deg);
    }
    100%{
        opacity: 0;
        transform: translateY(-1000%) rotate(45deg);
    }
}

.seconds-label {
    margin-top: 32px;
    font-size: 32px;
    font-family: "Monda_no", Georgia, sans-serif;
    width: auto;
    text-align: center;
}

.container {
    position: absolute;
}
.days {
    font-size: 40px;
    width: auto;
    text-align: center;
}
.words,
.days-number,
.seconds {
    font-size: 20px;
    text-align: center;
    color: #cc2a5d;
}

.words {
  padding-top: 26px;
  white-space: nowrap;
}

.days-number {
  margin: 16px 0;
  font-size: 30px;
}

.center-box {
   margin-top: 150px;
}

.animate__animated {
  /* animation-name:flash;
  animation-duration: 3s; */
}

button {
  /* position: absolute; */
  /* left: -100px; */
}

.button3 {
    width: 200px;
    height: 46px;
    line-height: 46px;
    background-color: #2e82ff;
    color: #ffffff;
    font-size: 18px;
    text-align: center;
    border-radius: 27px;
    position: relative;
}
.button3::before {
    content: "";
    position: absolute;
    left: 0px;
    width: 100%;
    height: 100%;
    background-image: 
        linear-gradient(to right, rgba(255, 255, 255, 0) 30%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0) 70%);
    background-size: 200%;
    animation: wipes 1s infinite;
}
@keyframes wipes {
    0% {
        background-position: 0 0;
    }
    100% {
        background-position: 100% 0;
    }
}

</style>
